<?php
class adm_user_model extends CI_Model {


	public function __construct()
	{
		parent::__construct();
		$this->db = $this->load->database('default',true);
	}
	/**
	 *
	 *
		* @name check_login
		* @date 2013-1-9
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description 根据用户名查询用户ID,密码,连续错误次数,状态
	 */
	public function check_login($user_name) {
		$user_name	=$this->db->escape($user_name);
		$sql =	"select user_id,password,error_times,status
					from adm_user
					where user_name =$user_name
					limit 1";
		$query =$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}

	/**
	 *
	 *
	 * @name add_error_times
		* @date 2013-1-9
		* @author eifel
		* @param 用户ID,连续错误次数
		* @return
		* @version 1.0
		* @description 修改用户连续登录失败的次数
	 */
	public function add_error_times($user_id,$times) {
		$user_id	=$this->db->escape($user_id);
		$times		=$this->db->escape($times);

		$sql ="update adm_user set error_times=$times where user_id =$user_id";
		$this->db->query($sql);
	}
	/**
	 *
	 *
		* @name
		* @date 2013-1-9
		* @author eifel
		* @param
		* @return
		* @version 1.0
		* @description
	 */

	public function get_error_times($user_name) {
		$user_name	=$this->db->escape($user_name);
		$sql =	"select error_times
				from adm_user
				where user_name =$user_name
				limit 1";
		$query =$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}

	/**
	 *
	 *
		* @name get_user_info
		* @date 2013-1-30
		* @author eifel
		* @param string 用户编号
		* @return array 用户详情
		* @version 1.0
		* @description 根据用户编号查询用户详情
	 */
	public function get_user_info($user_id) {
		$user_id 	=$this->db->escape($user_id);
		$sql		="select * from adm_user where user_id = $user_id limit 1";
		$query 		=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}

	/**
	 *
	 *
		* @name change_password
		* @date 2013-1-30
		* @author eifel
		* @param string 用户名,string密码
		* @return
		* @version 1.0
		* @description 修改密码
	 */
	public function change_password($user_name,$password) {
		$user_name	=$this->db->escape($user_name);
		$password	=$this->db->escape($password);
		$sql		="update adm_user set password =$password where user_name =$user_name";
		$this->db->query($sql);
	}
	


	/**
	 *
	 *
	 * @name query_user_by_name
	 * @date 2013-3-26
	 * @author eifel
	 * @param string 用户名
	 * @return array 用户信息
	 * @version 1.0
	 * @description 通过用户名查询用户信息
	 */
	public function query_user_by_name($user_name) {
		$user_name	=$this->db->escape($user_name);
		$sql	="select user_id from adm_user where user_name =$user_name limit 1";
		$query	=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}

	/**
	 *
	 *
	 * @name add_user
	 * @date 2013-3-26
	 * @author eifel
	 * @param array 用户信息
	 * @return string 用户编号
	 * @version 1.0
	 * @description 插入用户信息并返回用户编号
	 */
	public function add_user($user_data) {
		$data=array("user_name"	=>$user_data['user_name'],
					"name"		=>$user_data['name'],
					"password"	=>$user_data['password'],
					"email"		=>$user_data['email'],
					"sex"		=>$user_data['sex'],
					"dept_id"	=>$user_data['dept_id'],
					"dept_id1"	=>$user_data['dept_id1'],
					"dept_id2"	=>$user_data['dept_id2'],
					"created_by"=>$user_data['created_by'],
					"created_time"=>$user_data['created_time'],
		);
		$this->db->insert("adm_user",$data);
		return $this->db->insert_id();
	}

	/**
	 *
	 *
	 * @name query_user_list
	 * @date 2013-3-26
	 * @author eifel
	 * @param int 起始数,int 分页数
	 * @return array 用户列表
	 * @version 1.0
	 * @description 查询用户列表
	 */
	public function query_user_list($start,$limit) {
		$sql ="select user_name,adm_user.user_id,dept_name,name
		 		from 
				 adm_user left join adm_dept as ad1 on adm_user.dept_id=ad1.dept_id 
				 order by 
				user_id desc limit $start,$limit ";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}

	/**
	 *
	 *
	 * @name query_user_num
	 * @date 2013-3-26
	 * @author eifel
	 * @param
	 * @return int 用户数量
	 * @version 1.0
	 * @description 查询用户数量
	 */
	public function query_user_num() {
		$sql 	="select user_id from adm_user ";
		$query	=$this->db->query($sql);
		return $query->num_rows();
	}

	/**
	 *
	 *
	 * @name query_user_detail
	 * @date 2013-3-26
	 * @author eifel
	 * @param string 用户编号
	 * @return  array 用户信息
	 * @version 1.0
	 * @description 通过用户编号查询用户基本信息和角色信息
	 */
	public function query_user_detail($user_id) {
		$sql ="select 	adm_user.*,
						ad1.dept_name as dept_name1,
						ad2.dept_name as dept_name2,
						ad3.dept_name as dept_name3
				from 	
						adm_user 
				 		left join adm_dept as ad1 on adm_user.dept_id=ad1.dept_id 
				 		left join adm_dept as ad2 on adm_user.dept_id1=ad2.dept_id 
				 		left join adm_dept as ad3 on adm_user.dept_id2=ad3.dept_id 	
				where adm_user.user_id  ='$user_id' limit 1 ";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->row_array();
		}
	}

	/**
	 *
	 *
	 * @name modify_user
	 * @date 2013-3-27
	 * @author eifel
	 * @param array 用户信息,string 用户编号
	 * @return
	 * @version 1.0
	 * @description 编辑用户信息
	 */
	public function modify_user($user_data,$user_id) {
		$data=array("user_name"	=>$user_data['user_name'],
					"name"		=>$user_data['name'],
					"email"		=>$user_data['email'],
					"sex"		=>$user_data['sex'],
					"dept_id"	=>$user_data['dept_id'],
					"dept_id1"	=>$user_data['dept_id1'],
					"dept_id2"	=>$user_data['dept_id2'],
					"modified_by"=>$user_data['modified_by'],
					"modified_time"=>$user_data['modified_time'],
		);
		$this->db->where("user_id",$user_id);
		$this->db->update("adm_user",$data);
	}

	/**
	 *
	 *
	 * @name delete_user
	 * @date 2013-3-27
	 * @author eifel
	 * @param string 用户编号
	 * @return
	 * @version 1.0
	 * @description
	 */
	public function delete_user($user_id) {
		$user_id 	=$this->db->escape($user_id);
		$sql 		="delete from adm_user where user_id =$user_id";
		$this->db->query($sql);
	}

	/**
	 *
	 *
	 * @name search_user
	 * @date 2013-3-26
	 * @author eifel
	 * @param string 名称关键词
	 * @return array 用户列表
	 * @version 1.0
	 * @description 根据名称和真实姓名搜索角色
	 */
	public function search_user($key) {
		$key 	=$this->db->escape_like_str($key);
		$sql ="select user_name,adm_user.user_id,role_name,name from adm_user
				left join adm_user_role on adm_user.user_id=adm_user_role.user_id
				left join adm_role  on adm_role.role_id =adm_user_role.role_id 
				where adm_user.user_name like '%$key%' or adm_user.name like '%$key%'";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0) {
			return $query->result_array();
		}
	}
	
	public function update_user_token($user_id,$token){
		$sql ="update adm_user set token='$token' where user_id ='$user_id'";
		$query =$this->db->query($sql);
	}
	
	public function query_user_id_by_token($token){
		$token 	=$this->db->escape($token);
		$sql 	="select user_id from adm_user where token=$token limit 1 ";
		$query	=$this->db->query($sql);
		return $query->row_array();
	}
	public function mobile_logout($user_id){
		$user_id 	=$this->db->escape($user_id);
		$sql ="update adm_user set token='' where user_id =$user_id";
		$query	=$this->db->query($sql);
	}
}